// 商品分类
<template>
  <div>
    <Row class="page">
      <Col span="24">
      <Modal v-model="modelDel" title="提醒" @on-ok="delok" @on-cancel="delcancel">
        <p>是否删除此活动，点击确定删除，点击取消不删除！</p>
      </Modal>
      <Button type="primary" icon="plus-round" @click="modal1 = true">添加商品活动</Button>
      <Modal v-model="modal1" title="添加商品活动" @on-ok="ok" @on-cancel="cancel">
        <Form :model="formItem" :label-width="100">
          <FormItem label="活动名称">
            <Input v-model="formItem.name" placeholder="活动名称" disabled></Input>
          </FormItem>
          <FormItem label="活动时间">
            <DatePicker type="datetimerange" placement="bottom-end" placeholder="选择时间" style="width: 100%" @on-change="changeData"></DatePicker>
          </FormItem>
          <FormItem label="团购人数">
            <Input v-model="formItem.group_number" placeholder="团购人数"></Input>
          </FormItem>
          <FormItem label="拼团价格">
            <Input v-model="formItem.group_price" placeholder="拼团价格"></Input>
          </FormItem>
          <FormItem label="是否首页显示">
            <i-switch v-model="switch1" @on-change="changes" />
          </FormItem>
          <FormItem label="商品ID">
            <Input v-model="formItem.goods_id" placeholder="商品ID"></Input>
            <br>
            <Select @on-change="changeTypes" :label-in-value="true" placeholder="选择商品">
              <Option v-for="item in worklist" :value="item.id" :key="item.id" :label="item.name"></Option>
            </Select>
          </FormItem>
          <FormItem label="选择活动">
            <Select v-model="formItem.type_id" @on-change="changeType" placeholder="选择活动">
              <Option v-for="item in list" :value="item.id" :key="item.id">{{item.name}}</Option>
            </Select>
          </FormItem>
        </Form>
      </Modal>
      </Col>
    </Row>
    <br>
    <Row class="page">
      <Col span="24">
      <Table border :columns="columns7" :data="data6"></Table>
      </Col>
    </Row>
    <br>
    <Row>
      <Page :total="100" @on-change="page" />
    </Row>
  </div>
</template>

<script type="text/ecmascript-6">
export default {
  data() {
    return {
      modelDel: false,
      delOrderId: "",
      workNames: "",
      pages: 1,
      infoType: true,
      switch1: false,
      worklist: [],
      list: [],
      index: 1,
      formItem: {
        name: "", //活动名称
        goods_id: "", //商品ID
        start_datetime: "", //开始时间
        end_datetime: "", //结束时间
        group_number: "", //库存
        group_price: "", //拼团价格
        is_sale: "", //是否启用
        group_times: 1, //拼团次数
        create_user: "root", //发布人
        type_id: "" //活动类型ID，对应焦点图、等活动页面
      },
      modal1: false,
      columns7: [
        {
          title: "活动ID",
          key: "id",
          width: 100
        },
        {
          title: "商品ID",
          key: "goods_id",
          width: 100,
          sortable: true
        },
        {
          title: "活动名称",
          key: "name"
        },
        {
          title: "拼团人数",
          key: "group_number",
          sortable: true
        },
        {
          title: "展示价格",
          key: "group_price",
          sortable: true
        },
        {
          title: "开始时间",
          key: "start_datetime",
          sortable: true
        },
        {
          title: "结束时间",
          key: "end_datetime",
          sortable: true
        },
        {
          title: "活动状态",
          key: "is_sale",
          sortable: true,
          render: (h, parmas) => {
            if (parmas.row.is_sale == 1) {
              return "已开启";
            } else {
              return "已停止";
            }
          }
        },
        {
          title: "操作",
          // key: "id",
          width: 150,
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "success",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      let _self = this;
                      $.ajax({
                        url: BASE_URL + "activitymanagement/edit",
                        type: "get",
                        data: {
                          id: params.row.id
                        },
                        success: function(e) {
                          console.log(e);
                          _self.infoType = false;
                          _self.formItem = e;
                          if (e.is_sale == 1) {
                            _self.switch1 = true;
                          } else {
                            _self.switch1 = false;
                          }
                          _self.modal1 = true;
                        }.bind(this)
                      });
                    }
                  }
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  style: {
                    marginLeft: "10px"
                  },
                  props: {
                    type: "error",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      let _self = this;
                      _self.modelDel= true
                      _self.delOrderId = params.row.id;
                      // $.ajax({
                      //   url: BASE_URL + "activitymanagement/delete",
                      //   type: "get",
                      //   data: {
                      //     id: params.row.id
                      //   },
                      //   success: function(e) {
                      //     console.log(e);
                      //     _self.getPage(_self.pages);
                      //     _self.$Message.info("删除成功");
                      //     _self.page(_self.pages);
                      //   }
                      // });
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        }
      ],
      data6: [],
      value: ""
    };
  },
  created() {
    this.getPage(1);
    this.getActicities();
    this.getWorkList();
  },
  methods: {
    delWork: function(orderid) {
      let _self = this;
      $.ajax({
        url: BASE_URL + "activitymanagement/delete",
        type: "get",
        data: {
          id: orderid
        },
        success: function(e) {
          console.log(e);
          _self.getPage(_self.pages);
          _self.$Message.info("删除成功");
          _self.page(_self.pages);
        }.bind(this)
      });
    },
    delok: function() {
      let _self = this;
      _self.delWork(_self.delOrderId);
    },
    delcancel:function(){
this.$Message.error('取消删除')
    },
    getActicities: function() {
      let _self = this;
      $.ajax({
        url: BASE_URL + "activitymanagement/groupActivityTypes",
        type: "get",
        success: function(e) {
          console.log(e);
          _self.list = e;
        }.bind(this)
      });
    },
    page: function(e) {
      console.log(e);
      this.pages = e;
      this.getPage(e);
    },
    getPage: function(pageNumner) {
      let _self = this;
      $.ajax({
        url: BASE_URL + "activitymanagement",
        type: "get",
        data: {
          pageNumber: pageNumner,
          pageSize: 10
        },
        success: function(data) {
          console.log(data);
          _self.data6 = data.list;
        }.bind(this)
      });
    },
    changeType: function(e) {
      console.log(e);
    },
    changeTypes: function(e) {
      this.formItem.goods_id = e.value;
      this.formItem.name = e.label;
      console.log(this.formItem.goods_id);
    },
    getWorkList: function() {
      let _self = this;
      $.ajax({
        url: BASE_URL + "goodsmanagement",
        type: "get",
        success: function(data) {
          console.log(data);
          _self.worklist = data;
        }.bind(this)
      });
    },
    changes: function(switch1) {
      // this.formItem.is_sale = switch1;

      console.log(this.switch1);
    },
    changeData: function(e) {
      console.log(e);
      this.formItem.start_datetime = e[0];
      this.formItem.end_datetime = e[1];
    },
    s1: function(e) {
      console.log(e);
    },
    ss: function(e) {
      console.log(e);
    },
    handleSelectAll(status) {
      this.$refs.selection.selectAll(status);
      console.log(this.$refs);
    },
    cancel() {
      console.log("asdfasdf");
      this.$Message.info("Clicked cancel");
      this.getWorkList();
      this.modal1 = false;
      this.formItem = {
        name: "", //活动名称
        goods_id: "", //商品ID
        start_datetime: "", //开始时间
        end_datetime: "", //结束时间
        group_number: "", //库存
        group_price: "", //拼团价格
        is_sale: "", //是否启用
        group_times: 1, //拼团次数
        create_user: "root", //发布人
        type_id: "" //活动类型ID，对应焦点图、等活动页面
      };
      this.switch1 = false;

      console.log(this.formItem);
    },
    ok: function() {
      let _self = this;
      // console.log(_self.formItem);

      if (_self.infoType) {
        if (_self.switch1) {
          this.formItem.is_sale = "1";
        } else {
          this.formItem.is_sale = "0";
        }
        console.log(_self.formItem);
        $.ajax({
          url: BASE_URL + "activitymanagement",
          type: "post",
          data: {
            groupActivity: JSON.stringify(_self.formItem)
          },
          success: function(e) {
            console.log(e);

            _self.getPage(_self.pages);
            this.formItem = {
              name: "", //活动名称
              goods_id: "", //商品ID
              start_datetime: "", //开始时间
              end_datetime: "", //结束时间
              group_number: "", //库存
              group_price: "", //拼团价格
              is_sale: "", //是否启用
              group_times: 1, //拼团次数
              create_user: "root", //发布人
              type_id: "" //活动类型ID，对应焦点图、等活动页面
            };
            this.switch1 = false;
          }.bind(this)
        });
      } else {
        if (_self.switch1) {
          this.formItem.is_sale = "1";
        } else {
          this.formItem.is_sale = "0";
        }
        console.log(_self.formItem);
        $.ajax({
          url: BASE_URL + "activitymanagement/update",
          type: "get",
          data: {
            groupActivity: JSON.stringify(_self.formItem)
          },
          success: function(e) {
            console.log(e);
            _self.getPage(_self.pages);
            _self.infoType = true;
            _self.formItem = "";
            this.switch1 = false;
          }.bind(this)
        });
      }
    }
  }
};
</script>

<style scoped>
</style>
